package com.movecar.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.movecar.R;
import com.movecar.model.OrderMessageBean;
import com.movecar.utils.AppConfig;
import com.movecar.utils.imageloader.Constants;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * An array adapter that knows how to render views when given CustomData classes
 */
public class AddShareGroupAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<OrderMessageBean> dataList = new ArrayList<OrderMessageBean>();
	private ImageLoader imageLoader = null;
	
	private Activity activity;

	public static Map<Integer, Boolean> isSelected;

	public AddShareGroupAdapter(Activity activity, List<OrderMessageBean> dataList) {
		this.activity = activity;
		this.dataList = dataList;
		mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = ImageLoader.getInstance();
		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < dataList.size(); i++) {
			isSelected.put(i, false);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		final ViewHolder viewHolder;

		if (view == null) {
			viewHolder = new ViewHolder();
			view = mInflater.inflate(R.layout.add_share_gridview_item, null);

			viewHolder.order_img = (ImageView) view.findViewById(R.id.order_img);
			viewHolder.order_title = (TextView) view.findViewById(R.id.order_title);
			viewHolder.coins = (TextView) view.findViewById(R.id.coins);
			viewHolder.choose = (CheckBox) view.findViewById(R.id.choose);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.order_title.setText(dataList.get(position).getMsgTitle());
		viewHolder.coins.setText("奖励云豆" + dataList.get(position).getShareCoin());
		imageLoader.displayImage(AppConfig.HOST + dataList.get(position).getSeLogo(), viewHolder.order_img,
				Constants.image_display_options, AppConfig.animateListener);
		viewHolder.choose.setChecked(isSelected.get(position));
		return view;
	}

	/** View holder for the views we need access to */
	public class ViewHolder {

		public ImageView order_img;
		public TextView order_title, coins;
		public CheckBox choose;
	}

}
